package com.fanhao.dllo.justforyou.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.FoodShowAdapter;
import com.fanhao.dllo.justforyou.view.SpacesItemDecoration;
import com.fanhao.dllo.justforyou.view.SwipeRefreshLoadingLayout;
import com.fanhao.dllo.justforyou.entity.FoodShowResult;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodShowActivity extends AppCompatActivity
        implements SwipeRefreshLoadingLayout.OnRefreshListener,
        SwipeRefreshLoadingLayout.OnLoadListener, View.OnClickListener {
    private ImageView backIv;
    private RecyclerView recyclerView;
    private FoodShowAdapter foodShowAdapter;
    private RequestQueue queue;

    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_show);

        initView();
        initData();
        recyclerView.setAdapter(foodShowAdapter);

        swipeRefreshLoadingLayout.setOnRefreshListener(this);
        swipeRefreshLoadingLayout.setOnLoadListener(this);
    }

    private void initView() {


        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);


        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL));
        foodShowAdapter = new FoodShowAdapter();


        SpacesItemDecoration decoration = new SpacesItemDecoration(20);
        recyclerView.addItemDecoration(decoration);

        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout)
        findViewById(R.id.swipeRefreshLoadingLayout);

        backIv = (ImageView)findViewById(R.id.backIv);
        backIv.setOnClickListener(this);


    }


    private void initData() {
        queue = Volley.newRequestQueue(this);
        String url ="http://api.meishixing.com/picture/picturelist/last/page=1&" +
        "city_id=7&session_id=00001227993a5661b25da19c79c2a7c9a6affc";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                      swipeRefreshLoadingLayout.setRefreshing(false);
                      Gson gson = new Gson();

                        FoodShowResult fsr = gson.fromJson(response,
                                FoodShowResult.class);

                        if (fsr != null) {
                            List<FoodShowResult.ResultEntity> resultEntity = fsr.getResult();
                            if (resultEntity != null) {
                                Log.i("lanou",resultEntity+"jjjjjjjj");
                                foodShowAdapter.addData(resultEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<>();
                map.put("Accept-Encoding","");
                return map;
            }
        };

        queue.add(request);
    }


    @Override
    public void onRefresh() {
     initData();
    }

    @Override
    public void onLoad() {
        loadData();
    }

    private void loadData() {

        queue = Volley.newRequestQueue(this);
        String url ="http://api.meishixing.com/picture/picturelist/like_most/page=1&city_id=7&" +
                "session_id=00001227993a5661b25da19c79c2a7c9a6affc&&lng=38.88291&lat=121.53974";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        swipeRefreshLoadingLayout.setLoading(false);
                        Gson gson = new Gson();

                        FoodShowResult fsr = gson.fromJson(response,
                                FoodShowResult.class);

                        if (fsr != null) {
                            List<FoodShowResult.ResultEntity> resultEntity = fsr.getResult();
                            if (resultEntity != null) {
                                Log.i("lanou",resultEntity+"jjjjjjjj");
                                foodShowAdapter.loadData(resultEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<>();
                map.put("Accept-Encoding","");
                return map;
            }
        };

        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}

